﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Utility;
using VAdvantage.DataBase;
using System.Windows.Threading;
using VAdvantage.Classes;
using VAdvantage.WorkSpace;

namespace VAdvantage.Framework
{
    public class Subscribe
    {

        //public void Insert(int win_ID, int rec_ID, int tab_ID)
        //{
        //    //string msg = string.Empty;
        //    //System.Threading.ThreadPool.QueueUserWorkItem(delegate
        //    //{
           
            
        //    // int flag = CheckSubscription(win_ID ,rec_ID ,tab_ID);
        //    // if (flag == 0)
        //    //  {




        //    //    int ID = DB.GetNextID(Envs.GetCtx().GetAD_Client_ID(), "CM_Subscribe", null);

        //    //    string sql = "Insert into CM_Subscribe (AD_CLIENT_ID,AD_ORG_ID,CM_SUBSCRIBE_ID,ISACTIVE,AD_WINDOW_ID,AD_TABLE_ID,RECORD_ID,CreatedBy,AD_USER_ID) values" +
        //    //    "(" + Envs.GetCtx().GetAD_Client_ID() + "," + Envs.GetCtx().GetAD_Org_ID() + "," + ID + ",'Y'," + win_ID + "," + tab_ID + "," + rec_ID + "," + Envs.GetCtx().GetAD_User_ID() + "," + Envs.GetCtx().GetAD_User_ID() + ")";

        //    //    if (DB.ExecuteQuery(sql) == 1)
        //    //    {
        //    //        msg = "RecordSubscribedSucessfully";
        //    //        ShowMessage.Info(msg, true, "", "");
        //    //    }
        //    //    else
        //    //    {
        //    //        msg = "RecordSubscriptionFailed";
        //    //        ShowMessage.Info(msg, true, "", "");
        //    //    }
        //    //  }
        //    //else
        //    //{
        //    //    if (DB.ExecuteQuery("delete from CM_Subscribe where CM_Subscribe_ID=" + flag) == 1)
        //    //    {
        //    //        msg = "RecordUnSubscribedSucessfully";
        //    //        ShowMessage.Info(msg, true, "", "");
        //    //    }
        //    //    else
        //    //    {
        //    //        msg = "RecordUnSubscriptionFailed";
        //    //        ShowMessage.Info(msg, true, "", "");
        //    //    }
        //    //}

        //    //    //System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate
        //    //    //{
        //    //    //    ShowMessage.Info(msg, true, "", "");

        //    //    //});
        //    //});
        //}
        //public int CheckSubscription(int win_ID, int rec_ID, int tab_ID)
        //{
        //    string chkSubscription = "select CM_Subscribe_ID from CM_Subscribe where AD_TABLE_ID=" + tab_ID + " And RECORD_ID=" + rec_ID + " And AD_WINDOW_ID=" + win_ID + " And AD_USER_ID=" + Envs.GetCtx().GetAD_User_ID() + "   ";
        //    return Convert.ToInt32(DB.ExecuteScalar(chkSubscription)); 
        //}

        public void InsertSubscription(int win_ID, int rec_ID, int table_ID)
        {
            string msg = string.Empty;
            //System.Threading.ThreadPool.QueueUserWorkItem(delegate
            //{
                int ID = DB.GetNextID(Envs.GetCtx().GetAD_Client_ID(), "CM_Subscribe", null);

                string sql = "Insert into CM_Subscribe (AD_CLIENT_ID,AD_ORG_ID,CM_SUBSCRIBE_ID,ISACTIVE,AD_WINDOW_ID,AD_TABLE_ID,RECORD_ID,CreatedBy,AD_USER_ID) values" +
                "(" + Envs.GetCtx().GetAD_Client_ID() + "," + Envs.GetCtx().GetAD_Org_ID() + "," + ID + ",'Y'," + win_ID + "," + table_ID + "," + rec_ID + "," + Envs.GetCtx().GetAD_User_ID() + "," + Envs.GetCtx().GetAD_User_ID() + ")";

                if (DB.ExecuteQuery(sql) == 1)
                {
                    //msg = "RecordSubscribedSucessfully";
                    //ShowMessage.Info(msg, true, "", "");
                }
                else
                {
                    msg = "SubscriptionFailed";
                    ShowMessage.Info(msg, true, "", "");
                }
            //});
        }

        public void DeleteSubscription(int cm_Subscribe_ID)
        {
            string msg = string.Empty;
            //System.Threading.ThreadPool.QueueUserWorkItem(delegate
            //{
                if (DB.ExecuteQuery("delete from CM_Subscribe where CM_Subscribe_ID=" + cm_Subscribe_ID) == 1)
                {
                    //msg = "RecordUnSubscribedSucessfully";
                    //ShowMessage.Info(msg, true, "", "");
                }
                else
                {
                    msg = "UnSubscriptionFailed";
                    ShowMessage.Info(msg, true, "", "");
                }
          //  });
        }
    }

}
